import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)
const store = new Vuex.Store( {
  state: {
    userName: '',  // 用户名称
    userAvatar: '', // 用户头像
  },

  mutations: {
    // 更新用户名称
    updateUserName(state, payload) {
      state.userName = payload;
      sessionStorage.setItem('userName', payload);
    },

    // 更新用户头像
    updateUserAvatar(state, payload) {
      state.userAvatar = payload;
      sessionStorage.setItem('userAvatar', payload);
    }
  },

  actions: {
    initializeStore({ commit }) {
      let name = sessionStorage.getItem('userName');
      if (name) {
        commit('updateUserName', name);
      }

      let avatar = sessionStorage.getItem('userAvatar');
      if (avatar) {
        commit('updateUserAvatar', avatar);
      }
    }
  }


})

export default store;
